//
//  YXAddPartnerTableView.m
//  CreditCard
//
//  Created by 杨旭 on 2017/12/22.
//  Copyright © 2017年 杨旭. All rights reserved.
//

#import "YXAddPartnerTableView.h"

@interface YXAddPartnerTableView ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,strong) NSArray *titleArr;
@property (nonatomic ,strong) NSArray *contentArr;
@property (nonatomic ,strong) UIButton *codeBtn;
@property (nonatomic ,strong) UIView *footerView;
@property (nonatomic ,strong) UIButton *addPartnerBtn;
@end

@implementation YXAddPartnerTableView

- (NSArray *)titleArr {
    if (!_titleArr) {
        _titleArr = @[@"姓        名",@"手机号码",@"设置密码",@"确认密码",@"验  证  码"];
    }
    return _titleArr;
}

- (NSArray *)contentArr {
    if (!_contentArr) {
        _contentArr = @[@"请输入姓名",@"请输入手机号码",@"请输入密码",@"请输入密码",@"请输入验证码"];
    }
    return _contentArr;
}

- (UIButton *)codeBtn {
    if (!_codeBtn) {
        _codeBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _codeBtn.backgroundColor = color_globalColor;
        _codeBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [_codeBtn setTitle:@"获取验证码" forState:(UIControlStateNormal)];
        [_codeBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
        _codeBtn.layer.masksToBounds = YES;
        _codeBtn.layer.cornerRadius = 8;
        [_codeBtn addTarget:self action:@selector(codeBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _codeBtn;
}

- (void)codeBtnAction:(UIButton *)sender {
    
    UITextField *tf1 = [self viewWithTag:4001];
  
    //获取验证码
    NSString *str = [self valiMobile:tf1.text];
    
    if(str) {
        [self showTostMessage:str];
    }else {
        [self GCDTime];
        self.codeBtnClickBlock(tf1.text);
    }
    
}

- (UIView *)footerView {
    if (!_footerView) {
        _footerView = [[UIView alloc] initWithFrame:(CGRectMake(0, 0, kScreenWidth, kScreenHeight -  self.titleArr.count * 50-15))];
        [_footerView addSubview:self.addPartnerBtn];
    }
    return _footerView;
}

- (UIButton *)addPartnerBtn {
    if (!_addPartnerBtn) {
        _addPartnerBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _addPartnerBtn.frame = CGRectMake(20, 100, kScreenWidth - 40, 50);
        _addPartnerBtn.layer.masksToBounds = YES;
        _addPartnerBtn.layer.cornerRadius = 8.0f;
        _addPartnerBtn.backgroundColor = [UIColor py_colorWithHexString:@"#CBCCCD"];
        _addPartnerBtn.userInteractionEnabled = NO;
        [_addPartnerBtn setTitle:@"添加合伙人" forState:(UIControlStateNormal)];
        [_addPartnerBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
        [_addPartnerBtn addTarget:self action:@selector(addPartnerBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
        
    }
    return _addPartnerBtn;
    
}

- (void)addPartnerBtnAction:(UIButton *)sender {
    UITextField *tf1 = [self viewWithTag:4000];
    UITextField *tf2 = [self viewWithTag:4001];
    UITextField *tf3 = [self viewWithTag:4002];
    UITextField *tf4 = [self viewWithTag:4003];
    UITextField *tf5 = [self viewWithTag:4004];

    if (tf1.text.length == 0 || tf2.text.length == 0 || tf3.text.length == 0 || tf4.text.length == 0 || tf5.text.length == 0) {
        [self showTostMessage:@"请填写完整信息！"];
        
    }else {
        NSDictionary *dcit = @{@"userName":tf1.text,@"account":tf2.text,@"password":tf3.text,@"password2":tf4.text,@"smsCode":tf5.text};;
        
        if (self.selectAddBtnClick) {
            self.selectAddBtnClick(dcit);
        }
    }
}


- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    if (self = [super initWithFrame:frame style:style]) {
        self.backgroundColor = color_backColor;
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.dataSource = self;
        self.delegate = self;
        self.contentInset = UIEdgeInsetsMake(15, 0, 0, 0);
        self.tableFooterView = self.footerView;
        [self registerClass:[YXAddPartnerTableViewcell class] forCellReuseIdentifier:@"YXAddPartnerTableViewcell"];
    }
    return self;
}

#pragma mark - UITableView Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.titleArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YXAddPartnerTableViewcell *cell = [tableView dequeueReusableCellWithIdentifier:@"YXAddPartnerTableViewcell" forIndexPath:indexPath];
    cell.titleLab.text = self.titleArr[indexPath.row];
    cell.contentTF.placeholder = self.contentArr[indexPath.row];
    cell.contentTF.tag = 4000+ indexPath.row;
    if (indexPath.row == 4) {
        [cell.contentView addSubview:self.codeBtn];
        __weak typeof (cell) weakSelf = cell;
        [_codeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(weakSelf.mas_centerY);
            make.right.equalTo(weakSelf.mas_right).offset(-10);
            make.size.mas_equalTo(CGSizeMake(100, 36));
        }];
        
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    YXAddPartnerTableViewcell *cell = [tableView cellForRowAtIndexPath:indexPath];
    [cell.contentTF becomeFirstResponder];
}

//制作timeButton的属性文本
- (NSMutableAttributedString *)makeTimeButtonAttributed:(NSInteger)time{
    NSMutableAttributedString *string3 = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%li",time] attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    NSMutableAttributedString *string4 = [[NSMutableAttributedString alloc]initWithString:@"剩余" attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
    [string4 insertAttributedString:string3 atIndex:string4.length];
    return string4;
}
#pragma mark - time
- (void)GCDTime{
    
    __block NSInteger time = 60;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
    dispatch_source_set_event_handler(timer, ^{
        if (time < 1) {
            dispatch_source_cancel(timer);
            NSMutableAttributedString *string = [[NSMutableAttributedString alloc]initWithString:@"重新发送" attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
            dispatch_async(dispatch_get_main_queue(), ^{
                _codeBtn.userInteractionEnabled = YES;
                [_codeBtn setAttributedTitle:string forState:(UIControlStateNormal)];
                _codeBtn.backgroundColor = color_globalColor;
            });
        }else{
            dispatch_async(dispatch_get_main_queue(), ^{
                _codeBtn.userInteractionEnabled = NO;
                _codeBtn.backgroundColor = [UIColor py_colorWithHexString:@"#CBCCCD"];
                [_codeBtn setAttributedTitle:[self makeTimeButtonAttributed:time] forState:(UIControlStateNormal)];
                _addPartnerBtn.backgroundColor = color_globalColor;
                _addPartnerBtn.userInteractionEnabled = YES;
                
            });
            time --;
        }
    });
    dispatch_resume(timer);
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITextField *tf1 = [self viewWithTag:4000];
    UITextField *tf2 = [self viewWithTag:4001];
    UITextField *tf3 = [self viewWithTag:4002];
    UITextField *tf4 = [self viewWithTag:4003];
    UITextField *tf5 = [self viewWithTag:4004];
    [tf1 resignFirstResponder];
    [tf2 resignFirstResponder];
    [tf3 resignFirstResponder];
    [tf4 resignFirstResponder];
    [tf5 resignFirstResponder];
}


@end



@interface YXAddPartnerTableViewcell ()<UITextFieldDelegate>


@end

@implementation YXAddPartnerTableViewcell

- (UILabel *)titleLab {
    if (!_titleLab) {
        _titleLab = [UILabel setLabelWithText:@"" fontSize:16 color:color_textOne];
    }
    return _titleLab;
}

- (UITextField *)contentTF {
    if (!_contentTF) {
        _contentTF = [[UITextField alloc] init];
        _contentTF.textColor = color_globalColor;
        _contentTF.delegate = self;
    }
    return _contentTF;
}

- (UIView *)lineView {
    if (!_lineView) {
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor= color_lineColor;
    }
    return _lineView;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self.contentView addSubview:self.titleLab];
        [self.contentView addSubview:self.contentTF];
        [self.contentView addSubview:self.lineView];
    }
    return self;
}

#pragma mark -TextField代理方法
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    return [textField resignFirstResponder];
}
- (void)layoutSubviews {
    [super layoutSubviews];
    YXWeakSelf
    [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.mas_left).offset(10);
        make.centerY.equalTo(weakSelf.mas_centerY);
        [_titleLab sizeToFit];
    }];
    
    [_contentTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(weakSelf.mas_centerY);
        make.left.equalTo(weakSelf.mas_left).offset(120);
        make.size.mas_equalTo(CGSizeMake(150, 50));
    }];
    
    
    [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.mas_left).offset(0);
        make.right.equalTo(weakSelf.mas_right).offset(0);
        make.bottom.equalTo(weakSelf.mas_bottom).offset(0);
        make.size.mas_equalTo(CGSizeMake(kScreenWidth, 1));
    }];
}



@end





